package com.sc.nft.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import com.sc.nft.dao.*;
import com.sc.nft.entity.*;
import com.sc.nft.entity.dto.GasConsumeDTO;
import com.sc.nft.entity.vo.*;
import com.sc.nft.entity.vo.ecology.EcologyCastGasVO;
import com.sc.nft.enums.*;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.*;
import com.sc.nft.sup.ErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * 服务接口实现
 *
 * @author rdg
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2022-06-28 16:14:01
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class PrivilegeCollectionServiceImpl implements PrivilegeCollectionService {
    private final PrivilegeRuleDao privilegeRuleDao;
    private final PrivilegeGasConfDao privilegeGasConfDao;
    private final UserWalletDao userWalletDao;
    private final PrivilegeDao privilegeDao;
    private final DigitalCollectionDao digitalCollectionDao;
    private final UserCollectionService userCollectionService;
    private final UserConsensusService userConsensusService;
    private final UserCreatorInfoDao userCreatorInfoDao;
    private final UserCollectionDao userCollectionDao;
    private final StaticDataDao staticDataDao;
    private final UserSynthesisRecordDao synthesisRecordDao;
    private final DigitalCollectionProductInfoDao digitalCollectionProductInfoDao;
    private final ProductStockDao productStockDao;
    private final ProductStockService productStockService;
    private final UserSynthesisRecordDao userSynthesisRecordDao;
    private final UserSynthesisDetailsRecordDao userSynthesisDetailsRecordDao;
    private final FilterIncomeListDao filterIncomeListDao;
    private final OrderGasService orderGasService;

    @Override
    public List<PrivilegeProductVO> privilegeProductList(Integer commodityType) {
        if (ObjectUtil.isEmpty(commodityType) || commodityType < CommodityType.PRIVILEGE.getCode()) {
            commodityType = CommodityType.PRIVILEGE.getCode();
        }
        List<PrivilegeProductVO> privilegeProductVOS = digitalCollectionProductInfoDao.privilegeProductList(commodityType);
        for (PrivilegeProductVO privilegeProductVO : privilegeProductVOS) {
            if (privilegeProductVO.getShowLevel() == 0) {
                privilegeProductVO.setShowLevelName("");
            }
            String beginTime = privilegeProductVO.getBeginTime();
            if (ObjectUtil.isNotEmpty(beginTime)) {
                Date date = DateUtil.parse(beginTime, "yyyy-MM-dd HH:mm:ss");
                privilegeProductVO.setBeginTime(DateUtil.format(date, "MM-dd HH:mm"));
            }
            Privilege privilege = privilegeDao.getByCollectionId(privilegeProductVO.getRelationId());
            Integer count = 0;
            if (ObjectUtil.isNotEmpty(privilege)) {
                if (privilege.getIsAirdrop()) {
                    count++;
                }
                if (privilege.getFirstPayTime() > 0) {
                    count++;
                }
                if (privilege.getTransferDiscount().compareTo(BigDecimal.ZERO) == 1) {
                    count++;
                }
                if (privilege.getIsIpShare()) {
                    count++;
                }
            }
            if (privilegeProductVO.getUsableCount() <= 0) {
                privilegeProductVO.setProductType(3);
            }
            privilegeProductVO.setPrivilegeNum(count);
        }
        return privilegeProductVOS;
    }

    @Override
    public PrivilegeProductDetailVO privilegeProductDetail(Long id) {
        PrivilegeProductDetailVO privilegeProductDetailVO = new PrivilegeProductDetailVO();
        DigitalCollectionProductInfo digitalCollectionProductInfo = digitalCollectionProductInfoDao.getById(id);
        UserCreatorInfo userCreatorInfo = userCreatorInfoDao.getById(digitalCollectionProductInfo.getUserCreatorId());
        StaticData staticData = staticDataDao.getByType("synthesis_notes");
        UserInfo user = UserPool.getUser();
        //消耗的材料
        List<DestroyPrivilegeRuleVO> consumeList = privilegeRuleDao.getByProductIdAndDestory(digitalCollectionProductInfo.getId(), Boolean.TRUE);
//        List<DestroyPrivilegeRuleVO> noConsumeList = privilegeRuleDao.getByProductIdAndDestory(digitalCollectionProductInfo.getId(), Boolean.FALSE);
        Boolean isSynthesis = true;
        for (DestroyPrivilegeRuleVO ruleVO : consumeList) {
            Integer count = 0;
            if (ObjectUtil.isNotEmpty(user)) {
                count = userCollectionDao.getCountByCollectionId(ruleVO.getConsumeCollectionId(), user.getId());
            }
            ruleVO.setHavaingNum(count);
            if (count < ruleVO.getConsumeCollectionNum()) {
                isSynthesis = false;
            }
        }
        privilegeProductDetailVO.setId(id);
        privilegeProductDetailVO.setModelImg(digitalCollectionProductInfo.getModelImg());
        privilegeProductDetailVO.setModelDistance(digitalCollectionProductInfo.getModelDistance());
        privilegeProductDetailVO.setProductType(digitalCollectionProductInfo.getProductType());
        privilegeProductDetailVO.setPrivilegeName(digitalCollectionProductInfo.getFirstTitle());
        privilegeProductDetailVO.setCreationContent(digitalCollectionProductInfo.getCreationContent());
        privilegeProductDetailVO.setNickName(userCreatorInfo.getNickName());
        privilegeProductDetailVO.setCreatorAvatarImg(userCreatorInfo.getCreatorAvatarImg());
        privilegeProductDetailVO.setSynthesisNotes(staticData.getValue());
        privilegeProductDetailVO.setConsumeList(consumeList);
        privilegeProductDetailVO.setProductUuid(digitalCollectionProductInfo.getDigitalCollectionProductInfoUuid());
        privilegeProductDetailVO.setCatenaName(digitalCollectionProductInfo.getCatenaName());
        privilegeProductDetailVO.setProductDetailPicture(digitalCollectionProductInfo.getProductDetailPicture());
        privilegeProductDetailVO.setProductDetailPictureType(digitalCollectionProductInfo.getProductDetailPictureType());
        privilegeProductDetailVO.setThreeModelId(digitalCollectionProductInfo.getThreeModelId());
        privilegeProductDetailVO.setSkipUrl(digitalCollectionProductInfo.getSkipUrl());
        String value = "";
        if (digitalCollectionProductInfo.getCommodityType() == CommodityType.PRIVILEGE) {
            value = staticDataDao.getByType("synthesis_privilege_share_url").getValue() + "?productId=" + id;
        } else if (digitalCollectionProductInfo.getCommodityType() == CommodityType.SYNTHESIS) {
            value = staticDataDao.getByType("synthesis_product_share_url").getValue() + "?productId=" + id;
        }
        privilegeProductDetailVO.setProductShareUrl(value);
        Privilege privilege = privilegeDao.getByCollectionId(digitalCollectionProductInfo.getRelationId());
        String equityListStr = "";
        StringBuilder stringBuilder = new StringBuilder();
        if (ObjectUtil.isNotEmpty(privilege)) {
            if (privilege.getIsAirdrop()) {
                stringBuilder.append("1.活动空投");
            }
            if (privilege.getFirstPayTime() > 0) {
                stringBuilder.append("\n2.优先购特权 (每月" + privilege.getFirstPayTime()).append("次)");
            }
            if (privilege.getTransferDiscount().compareTo(BigDecimal.ZERO) == 1) {
                String s = privilege.getTransferDiscount().movePointRight(1).stripTrailingZeros().toPlainString();
                stringBuilder.append("\n3.手续费折扣 (" + s).append("折)");
            }
            if (privilege.getIsIpShare()) {
                stringBuilder.append("\n4.热门ip分成");
            }
            equityListStr = stringBuilder.toString();
        }
        privilegeProductDetailVO.setEquityListStr(equityListStr);
        privilegeProductDetailVO.setIsSynthesis(isSynthesis);
        ProductStock productStock = productStockDao.getProductStock(id, digitalCollectionProductInfo.getRelationId(), null);
        if (productStock.getUsableCount() <= 0) {
            privilegeProductDetailVO.setProductType(3);
        }
        privilegeProductDetailVO.setSumCount(productStock.getSumCount());
        privilegeProductDetailVO.setUsableCount(productStock.getUsableCount());
        Date endTime = digitalCollectionProductInfo.getEndTime();
        long offsetTime = 0L;
        if (ObjectUtil.isNotEmpty(endTime)) {
            DateTime dateTime = new DateTime(endTime);
            offsetTime = dateTime.between(DateTime.now(), DateUnit.MS);
        }
        privilegeProductDetailVO.setOffsetTime(offsetTime);
        return privilegeProductDetailVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SynthesisPrivilegeVO synthesisPrivilege(Long id) throws GlobalException {
        SynthesisPrivilegeVO synthesisPrivilegeVO = new SynthesisPrivilegeVO();
        UserInfo user = UserPool.getUser();
        //特权商品
        DigitalCollectionProductInfo digitalCollectionProductInfo = digitalCollectionProductInfoDao.getById(id);
        if (DateTime.now().isBefore(digitalCollectionProductInfo.getBeginTime()) || digitalCollectionProductInfo.getProductType() != 1) {
            throw new GlobalRunTimeException(ErrorCode.NOT_BEGIN);
        }
        if (ObjectUtil.isNotEmpty(digitalCollectionProductInfo.getEndTime()) && DateTime.now().isAfter(digitalCollectionProductInfo.getEndTime())) {
            throw new GlobalRunTimeException(ErrorCode.ALREADY_END);
        }
        ProductStock productStock = productStockDao.getProductStock(digitalCollectionProductInfo.getId(), digitalCollectionProductInfo.getRelationId(), null);
        if (productStock.getUsableCount() <= 0 || digitalCollectionProductInfo.getIsSoldOut()) {
            throw new GlobalRunTimeException(ErrorCode.COLLECTION_IS_SOLD_OUT);
        }
        if (!digitalCollectionProductInfo.getStatus()) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_HAS_BEEN_DESCONTINUED);
        }

        //大咖一个账号只能合成一次
//        if(digitalCollectionProductInfo.getTypeClassify() == ClassifyType.COLLECTION && digitalCollectionProductInfo.getRelationId() == 241){
        if (digitalCollectionProductInfo.getTypeClassify() == ClassifyType.COLLECTION && digitalCollectionProductInfo.getRelationId() == 291) {
            //判断是否是第一次合成
            Integer count = userSynthesisRecordDao.getCountByUserIdAndCollectionId(user.getId(), digitalCollectionProductInfo.getRelationId());
            if (count > 0) {
                throw new GlobalRunTimeException("该商品只能合成一次");
            }
        }

        //合成数量限制,如果是玛特的市值账号没有限制
        if (!filterIncomeListDao.checkListIsFilterAccount(Arrays.asList(6L, 7L, 131L, 145L), user.getId())) {
            Integer count = userSynthesisRecordDao.getCountByUserIdAndProductId(user.getId(), digitalCollectionProductInfo.getId());
            if (count >= digitalCollectionProductInfo.getSynthesisCount()) {
                throw new GlobalRunTimeException("超出合成数量");
            }
        }
        //对应的特权藏品
        DigitalCollection digitalCollection = digitalCollectionDao.getById(digitalCollectionProductInfo.getRelationId());
        //校验拥有的卡片是否满足合成条件
        Boolean aBoolean = checkSynthesis(digitalCollectionProductInfo.getId(), user.getId());
        if (!aBoolean) {
            throw new GlobalRunTimeException(ErrorCode.SYNTHESIS_CONDITIONS_NOT_MWT);
        }

        ArrayList<Long> updateIds = Lists.newArrayList();

        //合成条件
        List<PrivilegeRuleVO> ruleVOS = privilegeRuleDao.getByProductId(digitalCollectionProductInfo.getId());
        for (PrivilegeRuleVO ruleVO : ruleVOS) {
            if (ruleVO.getIsDestroy()) {
//                DigitalCollection consumeCollection = digitalCollectionDao.getEcologyCollectionById(ruleVO.getConsumeCollectionId());
//                if (ObjectUtil.isNotNull(consumeCollection)) {
//                    userConsensusService.adjustConsensusService(user.getId(), ruleVO.getConsumeCollectionId(), BigDecimal.valueOf(ruleVO.getConsumeCollectionNum()).negate(), UserConsensusTypeEnums.SYNTHESIS_CONSUME, ruleVO.getConsumeCollectionName(), null);
//                }
                List<UserCollection> countList = userCollectionDao.getUserCollectionListByCollectionIdAndUserIdAndSize(ruleVO.getConsumeCollectionId(), user.getId(), ruleVO.getConsumeCollectionNum());
                Assert.isTrue(countList.size() == ruleVO.getConsumeCollectionNum(), () -> new GlobalRunTimeException("材料不足"));

                for (UserCollection userCollection : countList) {

                    Assert.isTrue(userCollectionDao.deleteCollectionByUserIdCollectionIdAndIds(user.getId(), ruleVO.getConsumeCollectionId(), userCollection.getId()), () -> new GlobalRunTimeException(ErrorCode.THE_SYSTEM_IS_BUSY_PLEASE_TRY_AGAIN_LATER));

                    UserSynthesisDetailsRecord userSynthesisDetailsRecord = new UserSynthesisDetailsRecord();
                    userSynthesisDetailsRecord.setUserCollectionId(userCollection.getId());
                    userSynthesisDetailsRecord.setCollectionId(userCollection.getCollectionId());
                    userSynthesisDetailsRecord.setNumbering(userCollection.getNumbering());
                    userSynthesisDetailsRecord.setCollectionName(userCollection.getFirstTitle());
                    userSynthesisDetailsRecord.insert();
                    updateIds.add(userSynthesisDetailsRecord.getId());
                }
                //藏品流转表记录
                CollectionCirculatingRecord collectionCirculatingRecord = new CollectionCirculatingRecord();
                collectionCirculatingRecord.setUserId(user.getId());
                collectionCirculatingRecord.setCollectionId(ruleVO.getConsumeCollectionId());
                collectionCirculatingRecord.setReceivedCollectionType(ReceivedCollectionType.SYNTHESIS);
                collectionCirculatingRecord.setNumber(ruleVO.getConsumeCollectionNum());
                collectionCirculatingRecord.insert();
                //生态孵化藏品 并且配置了消耗共识度
                if (countList.get(0).getCollectionType() == DigitalMarketTypeEnum.ECOLOGY && ruleVO.getConsumeConsensus()) {
                    userConsensusService.adjustConsensusService(user.getId(), ruleVO.getConsumeCollectionId(), BigDecimal.valueOf(ruleVO.getConsumeCollectionNum()).negate(),
                            UserConsensusTypeEnums.SYNTHESIS_CONSUME, ruleVO.getConsumeCollectionName(), null);

                }
            }
        }
        //减去特权商品库存
        productStockService.subIntegralStock(id, digitalCollectionProductInfo.getRelationId(), null, 1, user.getId(), CommodityType.PRIVILEGE);

        PrivilegeGasConf privilegeGasConf = privilegeGasConfDao.getPrivilegeGasConfByProductId(productStock.getProductId());
        if (Objects.nonNull(privilegeGasConf) && privilegeGasConf.getGasConf()) {
            //合成上链费
            GasConsumeDTO gasConsume = new GasConsumeDTO();
            gasConsume.setConsumeCost(privilegeGasConf.getConsumeCost());
            gasConsume.setUserId(user.getId());
            gasConsume.setGasConsumeType(GasConsumeTypeEnum.BALANCE);
            gasConsume.setWalletDetailType(WalletDetailTypeEnum.GAS_PRIVILEGE);
            gasConsume.setProductId(productStock.getProductId());
            gasConsume.setCollectionId(digitalCollection.getId());
            gasConsume.setOrderGasType(OrderGasTypeEnum.GAS_PRIVILEGE);
            orderGasService.gasConsume(gasConsume);
        }
        if (digitalCollection.getCollectionType() == DigitalMarketTypeEnum.ECOLOGY) {
            userConsensusService.adjustConsensusService(user.getId(), digitalCollection.getId(), BigDecimal.ONE, UserConsensusTypeEnums.SYNTHESIS, digitalCollection.getFirstTitle(), null);
        }

        UserCollection userCollection = userCollectionService.addCollection(user.getId(), null, digitalCollection.getId(), IdUtil.getSnowflakeNextIdStr(), digitalCollection.getCreationContent(), digitalCollection.getLevel(), digitalCollection.getFirstTitle(),
                digitalCollection.getUserCreatorId(), ReceivedCollectionType.SYNTHESIS, null, null, null, true, digitalCollectionProductInfo.getId(), 0L, 0L, digitalCollectionProductInfo.getProductNum(), 0, null, null, AddCollectionEnum.STAIR, null, null, false);
        //插入合成记录
        UserSynthesisRecord userSynthesisRecord = new UserSynthesisRecord();
        userSynthesisRecord.setProductId(digitalCollectionProductInfo.getId());
        userSynthesisRecord.setCollectionId(digitalCollection.getId());
        userSynthesisRecord.setPrivilegeLevel(digitalCollection.getPrivilegeLevel());
        userSynthesisRecord.setUserId(user.getId());
        userSynthesisRecord.setCollectionName(digitalCollection.getFirstTitle());
        userSynthesisRecord.setUserCollectionId(userCollection.getId());
        userSynthesisRecord.setCollectionUuid(userCollection.getCollectionUuid());
        userSynthesisRecord.insert();

        if (updateIds.size() > 0) {
            Assert.isTrue(userSynthesisDetailsRecordDao.updateRecordIdByIds(updateIds, userSynthesisRecord.getId()), () -> new GlobalRunTimeException("修改失败"));
        }

        // 在合成指定的城主和居民的权益卡的时候,触发30天返现,对该订单进行初始化处理.
        //藏品流转表记录
        CollectionCirculatingRecord collectionCirculatingRecord = new CollectionCirculatingRecord();
        collectionCirculatingRecord.setUserId(user.getId());
        collectionCirculatingRecord.setCollectionId(digitalCollection.getId());
        collectionCirculatingRecord.setReceivedCollectionType(ReceivedCollectionType.SYNTHESIS);
        collectionCirculatingRecord.setUserSynthesisRecordId(userSynthesisRecord.getId());
        collectionCirculatingRecord.insert();
        synthesisPrivilegeVO.setCreationContent(digitalCollection.getCreationContent());
        synthesisPrivilegeVO.setFirstTitle(digitalCollection.getFirstTitle());
        return synthesisPrivilegeVO;
    }


    @Override
    public List<SynthesisRecordVO> synthesisRecord() {
        Long userId = UserPool.getUser().getId();
        List<SynthesisRecordVO> list = synthesisRecordDao.synthesisRecord(userId);
        for (SynthesisRecordVO synthesisRecordVO : list) {
            synthesisRecordVO.setLevelName(PrivilegeCollectionLevelEnum.getValue(synthesisRecordVO.getPrivilegeLevel()));
        }
        return list;
    }


    @Override
    public SynthesisCollectionDetailVO synthesisCollectionDetail(Long id) {
        SynthesisCollectionDetailVO synthesisCollectionDetailVO = new SynthesisCollectionDetailVO();
        DigitalCollectionProductInfo digitalCollectionProductInfo = digitalCollectionProductInfoDao.getById(id);
        UserCreatorInfo userCreatorInfo = userCreatorInfoDao.getById(digitalCollectionProductInfo.getUserCreatorId());
        StaticData staticData = staticDataDao.getByType("product_synthesis_notes");
        UserInfo user = UserPool.getUser();
        //消耗的材料
        List<DestroyPrivilegeRuleVO> consumeList = privilegeRuleDao.getByProductIdAndDestory(digitalCollectionProductInfo.getId(), Boolean.TRUE);
        List<DestroyPrivilegeRuleVO> noConsumeList = privilegeRuleDao.getByProductIdAndDestory(digitalCollectionProductInfo.getId(), Boolean.FALSE);
        Boolean isSynthesis = true;
        for (DestroyPrivilegeRuleVO ruleVO : consumeList) {
            Integer count = 0;
            if (ObjectUtil.isNotEmpty(user)) {
                count = userCollectionDao.getCountByCollectionId(ruleVO.getConsumeCollectionId(), user.getId());
            }
            ruleVO.setHavaingNum(count);
            if (count < ruleVO.getConsumeCollectionNum()) {
                isSynthesis = false;
            }
        }
        for (DestroyPrivilegeRuleVO destroyPrivilegeRuleVO : noConsumeList) {
            Integer count = 0;
            if (ObjectUtil.isNotEmpty(user)) {
                count = userCollectionDao.getCountByCollectionNotConsignId(destroyPrivilegeRuleVO.getConsumeCollectionId(), user.getId());
            }
            destroyPrivilegeRuleVO.setHavaingNum(count);
            if (count < destroyPrivilegeRuleVO.getConsumeCollectionNum()) {
                isSynthesis = false;
            }
        }
        synthesisCollectionDetailVO.setId(id);
        synthesisCollectionDetailVO.setModelImg(digitalCollectionProductInfo.getModelImg());
        synthesisCollectionDetailVO.setModelDistance(digitalCollectionProductInfo.getModelDistance());
        synthesisCollectionDetailVO.setProductType(digitalCollectionProductInfo.getProductType());
        synthesisCollectionDetailVO.setPrivilegeName(digitalCollectionProductInfo.getFirstTitle());
        synthesisCollectionDetailVO.setCreationContent(digitalCollectionProductInfo.getCreationContent());
        synthesisCollectionDetailVO.setNickName(userCreatorInfo.getDaoName());
        synthesisCollectionDetailVO.setCreatorAvatarImg(userCreatorInfo.getCreatorAvatarImg());
        synthesisCollectionDetailVO.setSynthesisNotes(staticData.getValue());
        synthesisCollectionDetailVO.setConsumeList(consumeList);
        synthesisCollectionDetailVO.setNoConsumeList(noConsumeList);
        synthesisCollectionDetailVO.setProductUuid(digitalCollectionProductInfo.getDigitalCollectionProductInfoUuid());
        synthesisCollectionDetailVO.setIsSynthesis(isSynthesis);
        synthesisCollectionDetailVO.setCatenaName(digitalCollectionProductInfo.getCatenaName());
        synthesisCollectionDetailVO.setProductDetailPicture(digitalCollectionProductInfo.getProductDetailPicture());
        synthesisCollectionDetailVO.setProductDetailPictureType(digitalCollectionProductInfo.getProductDetailPictureType());
        synthesisCollectionDetailVO.setThreeModelId(digitalCollectionProductInfo.getThreeModelId());
        synthesisCollectionDetailVO.setSkipUrl(digitalCollectionProductInfo.getSkipUrl());
        String value = "";
        if (digitalCollectionProductInfo.getCommodityType() == CommodityType.PRIVILEGE) {
            value = staticDataDao.getByType("synthesis_privilege_share_url").getValue() + "?productId=" + id;
        } else if (digitalCollectionProductInfo.getCommodityType() == CommodityType.SYNTHESIS) {
            value = staticDataDao.getByType("synthesis_product_share_url").getValue() + "?productId=" + id;
        }
        synthesisCollectionDetailVO.setProductShareUrl(value);
        ProductStock productStock = productStockDao.getProductStock(id, digitalCollectionProductInfo.getRelationId(), null);
        if (productStock.getUsableCount() <= 0) {
            synthesisCollectionDetailVO.setProductType(3);
        }
        synthesisCollectionDetailVO.setSumCount(productStock.getSumCount());
        synthesisCollectionDetailVO.setUsableCount(productStock.getUsableCount());
        Date endTime = digitalCollectionProductInfo.getEndTime();
        long offsetTime = 0L;
        if (ObjectUtil.isNotEmpty(endTime)) {
            DateTime dateTime = new DateTime(endTime);
            offsetTime = dateTime.between(DateTime.now(), DateUnit.MS);
        }
        synthesisCollectionDetailVO.setOffsetTime(offsetTime);
        synthesisCollectionDetailVO.setIsLockedPosition(digitalCollectionProductInfo.getIsLockedPosition());
        synthesisCollectionDetailVO.setLockedPositionType(digitalCollectionProductInfo.getLockedPositionType());
        synthesisCollectionDetailVO.setLockedPositionHour(digitalCollectionProductInfo.getLockedPositionHour());
        synthesisCollectionDetailVO.setLockedPositionTime(digitalCollectionProductInfo.getLockedPositionTime());
        synthesisCollectionDetailVO.setLockedPositionDescribe();

        PrivilegeGasConf privilegeGasConf = privilegeGasConfDao.getPrivilegeGasConfByProductId(id);
        if (Objects.nonNull(privilegeGasConf) && privilegeGasConf.getGasConf()) {
            EcologyCastGasVO ecologyCastGas = new EcologyCastGasVO();
            ecologyCastGas.setConsumeType(privilegeGasConf.getConsumeType());
            ecologyCastGas.setGasConf(privilegeGasConf.getGasConf());
            ecologyCastGas.setConsumeCost(privilegeGasConf.getConsumeCost());
            UserWallet userWallet = userWalletDao.getByUserId(user.getId());
            ecologyCastGas.setBalance(Objects.isNull(userWallet) ? BigDecimal.ZERO : userWallet.getBalance());
            synthesisCollectionDetailVO.setEcologyCastGas(ecologyCastGas);
        }
        return synthesisCollectionDetailVO;
    }

    public Boolean checkSynthesis(Long productId, Long userId) {
        List<PrivilegeRuleVO> ruleVOS = privilegeRuleDao.getByProductId(productId);
        for (PrivilegeRuleVO ruleVO : ruleVOS) {
            Integer count = userCollectionDao.getCountByCollectionNotConsignId(ruleVO.getConsumeCollectionId(), userId);
            if (count < ruleVO.getConsumeCollectionNum()) {
                return false;
            }
        }
        return true;
    }
}
